package com.csun.cmny.provider.controller.admin;


import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.device.csun.camera.entity.CsunCameraData;
import com.csun.cmny.provider.device.csun.gateway.entity.CsunGatewayData;
import com.csun.cmny.provider.exceptions.DmcBizException;
import com.csun.cmny.provider.model.domain.*;
import com.csun.cmny.provider.model.dto.patrol.PatrolPageQuery;
import com.csun.cmny.provider.model.dto.propertyManagement.*;
import com.csun.cmny.provider.model.enums.device.DeviceStatusEnum;
import com.csun.cmny.provider.model.enums.device.DmcDeviceStatusEnum;
import com.csun.cmny.provider.model.enums.device.DmcDeviceTypeEnum;
import com.csun.cmny.provider.model.vo.propertyManagement.PropertyDataVo;
import com.csun.cmny.provider.model.vo.propertyManagement.PropertyManagementDeviceVo;
import com.csun.cmny.provider.model.vo.propertyManagement.PropertyManagementStausVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.*;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.RedisKeyUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import jxl.Sheet;
import jxl.Workbook;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

@RestController
@RequestMapping(value = "/propertyManagement", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class DmcPropertyManagementController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private DmcPropertyManagementService propertyManagementService;
    @Resource
    private DmcDeviceService dmcDeviceService;
    @Resource
    private DmcCsunSleepBindService sleepBindService;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    @Resource
    private DmcWatchOldmanService watchOldmanService;
    @Resource
    private DmcPhoneLocationService phoneLocationService;
    @Resource
    private DmcCsunGatewayLocationService gatewayLocationService;
    @Resource
    private DmcCsunButtonLocationService buttonLocationService;
    @Resource
    private DmcCsunBodySensorLocationService bodySensorLocationService;
    @Resource
    private DmcRfidReaderLocationService rfidReaderLocationService;
    @Resource
    private DmcRfidLabelOldmanService rfidLabelOldmanService;
    @Resource
    private DmcAlarmLightLocationService alarmLightLocationService;
    @Resource
    private DmcCsunCameraLocationService cameraLocationService;
    @Resource
    private DmcBedsideScreenBindService bedsideScreenBindService;
    @Resource
    private DmcCsunHumidistatLocationService humidistatLocationService;
    @Resource
    private DmcCsunDoorLocationService doorLocationService;
    @Resource
    private DmcCsunGatewayBindService gatewayBindService;
    @Resource
    private DmcDeviceUserBindService userBindService;
    @Resource
    private DmcSleepCareBindService sleepCareBindService;
    @Resource
    private DmcCsunBodyGroupBindService dmcCsunBodyGroupBindService;
    @Resource
    private DmcPatrolGreatLocationService dmcPatrolGreatLocationServiceImp;
    @Resource
    private DmcPatrolGreatConfigService dmcPatrolGreatConfigServiceImp;
    @Resource
    private DmcPatrolPointLocationService dmcPatrolPointLocationServiceImp;
    @Resource
    private DmcYingShiCameraService dmcYingShiCameraService;

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "物管平台新增设备信息")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Wrapper addDevice(@RequestBody PropertyManagementDeviceSaveDto propertyManagementDeviceSaveDto) {


        //校验设备编号是否存在
        DmcDevice dmcDevice1 = new DmcDevice();
        dmcDevice1.setDeviceNo(propertyManagementDeviceSaveDto.getDeviceNo());
        List<DmcDevice> select = dmcDeviceService.select(dmcDevice1);
        if (PublicUtil.isNotEmpty(select)) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("device.no.error", null));
        }

        //新增物管以及设备数据
        propertyManagementDeviceSaveDto.setCreator(getLoginAuthDto().getUserName());
        propertyManagementDeviceSaveDto.setCreatorId(getLoginAuthDto().getUserId());

        int save = propertyManagementService.addDeviceAndPropertyManagement(propertyManagementDeviceSaveDto);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), save);
    }


    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "物管平台分页查询设备信息")
    @RequestMapping(value = "/selectQuery", method = RequestMethod.POST)
    public Wrapper selectQuery(@RequestBody ProperManagementSelectDto properManagementSelectDto) {


        List<PropertyManagementDeviceVo> propertyManagementDeviceVos = propertyManagementService.selectQuery(properManagementSelectDto);

        for (PropertyManagementDeviceVo propertyManagementDeviceVo : propertyManagementDeviceVos) {
            if (propertyManagementDeviceVo.getGroupId() != 0) {
                propertyManagementDeviceVo.setIsBindGroup(true);
            }
            if (propertyManagementDeviceVo.getBuyGroupId() != 0) {
                propertyManagementDeviceVo.setIsBuy(true);
            }
            //写入机构名称
            String groupName = (String) redisTemplate.opsForValue().get(RedisKeyUtil.getUacGroupName(propertyManagementDeviceVo.getBuyGroupId().toString()));
            propertyManagementDeviceVo.setGroupName(groupName);
        }

        PageInfo<PropertyManagementDeviceVo> pageInfo = new PageInfo<>(propertyManagementDeviceVos);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), pageInfo);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "批量删除设备")
    @RequestMapping(value = "/deletes", method = RequestMethod.POST)
    public Wrapper deletes(@RequestBody DeviceDeriveExcelDto deriveExcelDto) {


        for (Long id : deriveExcelDto.getDeviceIds()) {

            DmcDevice dmcDevice = dmcDeviceService.selectByKey(id);
            DmcPropertyManagement dmcPropertyManagement = propertyManagementService.selectByKey(id);
            //判定设备是否被机构绑定
            if (dmcDevice.getGroupId() != 0) {
                return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("device.delete.group.error", null) + ":" + dmcDevice.getDeviceNo());
            }
            //判定设备是否被机构购买
            if (dmcPropertyManagement.getBuyGroupId() != 0) {
                return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("device.delete.Buygroup.error", null) + ":" + dmcDevice.getDeviceNo());
            }
        }


        int result = dmcDeviceService.deleteManagements(deriveExcelDto.getDeviceIds());

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), result);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "修改设备")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Wrapper update(@RequestBody PropertyManagementDeviceUpdateDto propertyManagementDeviceUpdateDto) {

        //校验设备是否已经被机构绑定
        DmcPropertyManagement dmcPropertyManagement = propertyManagementService.selectByKey(propertyManagementDeviceUpdateDto.getDeviceId());
        if (dmcPropertyManagement.getBuyGroupId() != 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("device.update.bind.error", null));
        }

        //校验设备编号是否存在
        if (PublicUtil.isNotEmpty(propertyManagementDeviceUpdateDto.getDeviceNo())) {
            DmcDevice device = new DmcDevice();
            device.setDeviceNo(propertyManagementDeviceUpdateDto.getDeviceNo());
            List<DmcDevice> select = dmcDeviceService.select(device);
            if (PublicUtil.isNotEmpty(select)) {
                for (DmcDevice dmcDevice : select) {
                    if (!dmcDevice.getId().equals(propertyManagementDeviceUpdateDto.getDeviceId())) {
                        return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("excel.deviceNo.error", null));
                    }
                }
            }
        }

        //更改设备以及物管信息
        int result = propertyManagementService.updateDevice(propertyManagementDeviceUpdateDto);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), result);
    }


    @PostMapping(consumes = "multipart/form-data", value = "/toLead")
    @ApiOperation(httpMethod = "POST", value = "后台导入设备信息")
    public Wrapper save(HttpServletRequest request, Integer deviceType) throws ParseException {
        //设备类型不能为空
        if (PublicUtil.isEmpty(deviceType)) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("excel.deviceType.null", null));
        }

        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

        MultipartFile file = multipartRequest.getFile("file");

        Workbook wb = null;

        //校验文件是否为excel格式
        try {
            wb = Workbook.getWorkbook(file.getInputStream());
        } catch (Exception e) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("excel.file.error", null));
        }


        // 获得工作薄
        Sheet st = wb.getSheet(0);

        //校验是否填入了数据
        if (st.getRows() <= 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("excel.null.error", null));
        }

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        List<DmcDevice> devices = new ArrayList<>();
        List<DmcPropertyManagement> propertyManagements = new ArrayList<>();

        //对读取的参数循环校验
        for (int i = 1; i < st.getRows(); i++) {

            String deviceNodeviceNo = st.getCell(0, i).getContents().trim();
            String batch = st.getCell(3, i).getContents().trim();

            //设备编号不能为空
            if (PublicUtil.isEmpty(deviceNodeviceNo)) {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("excel.deviceNo.null", null));
            }
            //批次不能为空
            if (PublicUtil.isEmpty(batch)) {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("managent.batch.null", null));
            }


            //校验设备编号是否存在
            DmcDevice device = new DmcDevice();
            device.setDeviceNo(deviceNodeviceNo);
            List<DmcDevice> select = dmcDeviceService.select(device);
            if (PublicUtil.isNotEmpty(select)) {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("excel.deviceNo.error", null) + "-----" + deviceNodeviceNo);
            }


            //校验时间类参数


            String timeRegex1 = "(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|" +
                    "((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|" +
                    "((0[48]|[2468][048]|[3579][26])00))-02-29)$";


            if (PublicUtil.isNotEmpty(st.getCell(2, i).getContents().trim())) {
                if (!Pattern.matches(timeRegex1, st.getCell(2, i).getContents().trim())) {
                    return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("excel.date,error", null));
                }
            } else {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("managent.productionDate.null", null));
            }
            if (PublicUtil.isNotEmpty(st.getCell(6, i).getContents().trim())) {
                if (!Pattern.matches(timeRegex1, st.getCell(6, i).getContents().trim())) {
                    return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("excel.date,error", null));
                }
            }
            if (PublicUtil.isNotEmpty(st.getCell(7, i).getContents().trim())) {
                if (!Pattern.matches(timeRegex1, st.getCell(7, i).getContents().trim())) {
                    return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("excel.date,error", null));
                }
            }


        }

        //写入以及存储参数
        for (int i = 1; i < st.getRows(); i++) {

            String deviceNodeviceNo = st.getCell(0, i).getContents().trim();
            String deviceName = st.getCell(1, i).getContents().trim();
            Date productionDate = simpleDateFormat.parse(st.getCell(2, i).getContents().trim());
            String batch = st.getCell(3, i).getContents().trim();
            String customerNo = st.getCell(5, i).getContents().trim();
            String shipmentDate = st.getCell(6, i).getContents().trim();
            String initializeDate = st.getCell(7, i).getContents().trim();
            String deviceVersion = st.getCell(8, i).getContents().trim();
            String remark = st.getCell(9, i).getContents().trim();


            //将数据写入设备和物管实体类
            DmcDevice dmcDevice = new DmcDevice();
            DmcPropertyManagement dmcPropertyManagement = new DmcPropertyManagement();


            //写入设备数据
            dmcDevice.setDeviceType(deviceType);
            dmcDevice.setStatus(DmcDeviceStatusEnum.ENABLE.getKey());
            dmcDevice.setDeviceNo(deviceNodeviceNo);
            if (PublicUtil.isNotEmpty(deviceName)) {
                dmcDevice.setDeviceName(deviceName);
            }
            if (PublicUtil.isNotEmpty(deviceVersion)) {
                dmcDevice.setDeviceVersion(deviceVersion);
            }
            if (PublicUtil.isNotEmpty(remark)) {
                dmcDevice.setRemark(remark);
            }
            dmcDevice.setCreator(getLoginAuthDto().getUserName());
            dmcDevice.setCreatorId(getLoginAuthDto().getUserId());

            //新增设备信息
            dmcDeviceService.save(dmcDevice);


            dmcPropertyManagement.setDeviceId(dmcDevice.getId());
            dmcPropertyManagement.setProductionDate(productionDate);
            dmcPropertyManagement.setBatch(batch);
            dmcPropertyManagement.setManagentNo(deviceNodeviceNo + "-" + batch);
            if (PublicUtil.isNotEmpty(customerNo)) {
                dmcPropertyManagement.setCustomerNo(customerNo);
            }
            if (PublicUtil.isNotEmpty(shipmentDate)) {
                dmcPropertyManagement.setShipmentDate(simpleDateFormat.parse(shipmentDate));
            }
            if (PublicUtil.isNotEmpty(initializeDate)) {
                dmcPropertyManagement.setInitializeDate(simpleDateFormat.parse(initializeDate));
            }


            devices.add(dmcDevice);
            propertyManagements.add(dmcPropertyManagement);

        }

        int saves = 0;
        if (PublicUtil.isNotEmpty(propertyManagements)) {
            saves = propertyManagementService.saves(propertyManagements);
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), saves);
    }

    @PostMapping(consumes = "multipart/form-data", value = "/merchantToLead")
    @ApiOperation(httpMethod = "POST", value = "商家导入设备信息进行绑定")
    public Wrapper merchantToLead(HttpServletRequest request) throws Exception {


        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

        MultipartFile file = multipartRequest.getFile("file");

        Workbook wb = null;

        //校验文件是否为excel格式
        try {
            wb = Workbook.getWorkbook(file.getInputStream());
        } catch (Exception e) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("excel.file.error", null));
        }


        // 获得工作薄
        Sheet st = wb.getSheet(0);

        //校验是否填入了数据
        if (st.getRows() <= 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("excel.null.error", null));
        }

        ArrayList<String> deviceNos = new ArrayList<>();

        for (int i = 1; i < st.getRows(); i++) {

            String deviceNodeviceNo = st.getCell(0, i).getContents().trim();

            //检验是否读取出设备编号
            if (PublicUtil.isEmpty(deviceNodeviceNo)) {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("excel.deviceNo.null", null));
            }


            //校验设备编号是否存在
            DmcDevice device = new DmcDevice();
            device.setDeviceNo(deviceNodeviceNo);
            List<DmcDevice> select = dmcDeviceService.select(device);
            if (PublicUtil.isEmpty(select)) {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("excel.deviceNo.sql.null", null) + "-----" + deviceNodeviceNo);
            }


            for (DmcDevice dmcDevice : select) {

                //校验此设备是否被此机构购买
                DmcPropertyManagement dmcPropertyManagement = propertyManagementService.selectByKey(dmcDevice.getId());
                if (!dmcPropertyManagement.getBuyGroupId().equals(getLoginAuthDto().getGroupId())) {
                    return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("managent.device.error", null) + "-----" + deviceNodeviceNo);
                }
                //校验此设备是否被机构绑定
                if (dmcDevice.getGroupId() != 0) {
                    return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("device.bing.group.error", null) + "-----" + deviceNodeviceNo);
                }

            }

            deviceNos.add(deviceNodeviceNo);

        }


        //将设备与机构进行绑定
        int bind = dmcDeviceService.bindDeviceGroup(deviceNos, getLoginAuthDto().getGroupId());


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), bind);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "分页查询设备数据")
    @RequestMapping(value = "/selectDeviceData", method = RequestMethod.POST)
    public Wrapper selectDeviceData(@RequestBody SelectDeviceDataDto selectDeviceDataDto) {

        if (selectDeviceDataDto.getDeviceType() < 0 || selectDeviceDataDto.getDeviceType() > DmcDeviceTypeEnum.getList().size()) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("proper.deviceType.error", null));
        }


        List<PropertyDataVo> devicedataList = null;

        Integer deviceType = selectDeviceDataDto.getDeviceType();

        // List<PropertyDataVo>list=propertyManagementService.selectDeviceData(selectDeviceDataDto);

        //随访仪
        if (deviceType.equals(DmcDeviceTypeEnum.FOLLOW.getIndex())) {
            devicedataList = propertyManagementService.selectDeviceData(selectDeviceDataDto);
        }
        //孝心手环
        if (deviceType.equals(DmcDeviceTypeEnum.XX_WATCH.getIndex())) {
            devicedataList = watchOldmanService.selectDeviceData(selectDeviceDataDto);
           /* for (PropertyDataVo propertyDataVo : devicedataList) {
                propertyDataVo.setStatus(DeviceStatusEnum.OFFLINE.getKey());
                DmcWatchData dmcWatchData = (DmcWatchData) redisTemplate.opsForValue().get(RedisKeyUtil.getXxWatchKey(propertyDataVo.getDeviceNo()));
                if(PublicUtil.isNotEmpty(dmcWatchData)){
                    if (dmcWatchData.getStatus().equals(DeviceStatusEnum.ONLINE.getKey())){
                        propertyDataVo.setStatus(DeviceStatusEnum.ONLINE.getKey());
                    }
                }
            }*/

        }
        //睡眠卫士
        if (deviceType.equals(DmcDeviceTypeEnum.BS_SLEEP.getIndex())) {
            devicedataList = sleepCareBindService.selectDeviceData(selectDeviceDataDto);
        }
        //电话机
        if (deviceType.equals(DmcDeviceTypeEnum.ZN_PHONE.getIndex())) {
            devicedataList = phoneLocationService.selectDeviceData(selectDeviceDataDto);
        }
        //智能网关
        if (deviceType.equals(DmcDeviceTypeEnum.CSUN_GATEWAY.getIndex())) {
            devicedataList = gatewayLocationService.selectDeviceData(selectDeviceDataDto);
            for (PropertyDataVo propertyDataVo : devicedataList) {
                propertyDataVo.setStatus(DeviceStatusEnum.OFFLINE.getKey());
                CsunGatewayData deviceData = (CsunGatewayData) redisTemplate.opsForValue().get(RedisKeyUtil.getCsunGatewayKey(propertyDataVo.getDeviceNo()));
                if (PublicUtil.isNotEmpty(deviceData)) {
                    if (deviceData.getStatus().equals(DeviceStatusEnum.ONLINE.getKey())) {
                        propertyDataVo.setStatus(DeviceStatusEnum.ONLINE.getKey());
                    }
                }
            }
        }
        //紧急按钮
        if (deviceType.equals(DmcDeviceTypeEnum.CSUN_BUTTON.getIndex())) {
            devicedataList = buttonLocationService.selectDeviceData(selectDeviceDataDto);
        }
        //人体感应
        if (deviceType.equals(DmcDeviceTypeEnum.CSUN_BODY_SENSOR.getIndex())) {
            devicedataList = bodySensorLocationService.selectDeviceData(selectDeviceDataDto);
        }
        //门禁读卡器
        if (deviceType.equals(DmcDeviceTypeEnum.RFID_READER.getIndex())) {
            devicedataList = rfidReaderLocationService.selectDeviceData(selectDeviceDataDto);
        }
        //门禁标签
        if (deviceType.equals(DmcDeviceTypeEnum.RFID_LABEL.getIndex())) {
            devicedataList = rfidLabelOldmanService.selectDeviceData(selectDeviceDataDto);
        }
        //日创床垫
        if (deviceType.equals(DmcDeviceTypeEnum.CSUN_SLEEP.getIndex())) {
            devicedataList = sleepBindService.selectDeviceData(selectDeviceDataDto);
            for (PropertyDataVo propertyDataVo : devicedataList) {
                propertyDataVo.setStatus(DeviceStatusEnum.OFFLINE.getKey());
                /*CsunSleepData deviceData = (CsunSleepData) redisTemplate.opsForValue().get(RedisKeyUtil.getCsunSleepKey(propertyDataVo.getDeviceNo()));
                if (PublicUtil.isNotEmpty(deviceData)){
                    if (deviceData.getStatus().equals(DeviceStatusEnum.ONLINE.getKey())){
                        propertyDataVo.setStatus(DeviceStatusEnum.ONLINE.getKey());
                    }
                }*/
                if (redisTemplate.hasKey(RedisKeyUtil.getCsunSleepOnline(propertyDataVo.getDeviceNo()))) {
                    propertyDataVo.setStatus(DeviceStatusEnum.ONLINE.getKey());
                }
            }
        }
        //护工手表
        if (deviceType.equals(DmcDeviceTypeEnum.HG_WATCH.getIndex())) {
            devicedataList = propertyManagementService.selectDeviceData(selectDeviceDataDto);
        }
        //报警灯
        if (deviceType.equals(DmcDeviceTypeEnum.ALARM_LIGHT.getIndex())) {
            devicedataList = alarmLightLocationService.selectDeviceData(selectDeviceDataDto);
        }
        //智能摄像头
        if (deviceType.equals(DmcDeviceTypeEnum.CSUN_CAMERA.getIndex())) {
            devicedataList = cameraLocationService.selectDeviceData(selectDeviceDataDto);
            for (PropertyDataVo propertyDataVo : devicedataList) {
                propertyDataVo.setStatus(DeviceStatusEnum.OFFLINE.getKey());
                CsunCameraData deviceData = (CsunCameraData) redisTemplate.opsForValue().get(RedisKeyUtil.getCsunCameraKey(propertyDataVo.getDeviceNo()));
                if (PublicUtil.isNotEmpty(deviceData)) {
                    if (deviceData.getStatus().equals(DeviceStatusEnum.ONLINE.getKey())) {
                        propertyDataVo.setStatus(DeviceStatusEnum.ONLINE.getKey());
                    }
                }
            }
        }
        //床头屏
        if (deviceType.equals(DmcDeviceTypeEnum.BEDSIDE_SCREEN.getIndex())) {
            devicedataList = bedsideScreenBindService.selectDeviceData(selectDeviceDataDto);
        }
        //门禁控制卡
        if (deviceType.equals(DmcDeviceTypeEnum.CONTROLLER_CARD.getIndex())) {
            devicedataList = propertyManagementService.selectDeviceData(selectDeviceDataDto);
        }
        //温度计
        if (deviceType.equals(DmcDeviceTypeEnum.HUMIDISTAT.getIndex())) {
            devicedataList = humidistatLocationService.selectDeviceData(selectDeviceDataDto);
            for (PropertyDataVo propertyDataVo : devicedataList) {
                propertyDataVo.setStatus(DeviceStatusEnum.OFFLINE.getKey());
                Object deviceData = redisTemplate.opsForValue().get(RedisKeyUtil.getCsunHumidistatOnline(propertyDataVo.getDeviceNo()));

                if (PublicUtil.isNotEmpty(deviceData)) {
                    propertyDataVo.setStatus(DeviceStatusEnum.ONLINE.getKey());
                }
            }
        }
        //日创新人感
        if (deviceType.equals(DmcDeviceTypeEnum.NEWBODY.getIndex())) {
            devicedataList = bodySensorLocationService.selectDeviceData(selectDeviceDataDto);
            for (PropertyDataVo propertyDataVo : devicedataList) {
                propertyDataVo.setStatus(DeviceStatusEnum.OFFLINE.getKey());
                Object deviceData = redisTemplate.opsForValue().get(RedisKeyUtil.getCsunBodySensorOnline(propertyDataVo.getDeviceNo()));
                if (PublicUtil.isNotEmpty(deviceData)) {
                    propertyDataVo.setStatus(DeviceStatusEnum.ONLINE.getKey());
                }
            }
        }
        //日创门磁
        if (deviceType.equals(DmcDeviceTypeEnum.CSUN_DOOR.getIndex())) {
            devicedataList = doorLocationService.selectDeviceData(selectDeviceDataDto);
        }
        //巡更棒
        if (deviceType.equals(DmcDeviceTypeEnum.PATROL.getIndex())) {
            devicedataList = dmcPatrolGreatLocationServiceImp.selectDeviceData(selectDeviceDataDto);
        }
        //巡更点
        if (deviceType.equals(DmcDeviceTypeEnum.PATROL_POINT.getIndex())) {
            devicedataList = dmcPatrolPointLocationServiceImp.selectDeviceData(selectDeviceDataDto);
        }
        /**
         * @author chisj
         * @date 2021-10-28
         * @desc 新增萤石摄像头
         */
        if (deviceType.equals(DmcDeviceTypeEnum.YS_CAMERA.getIndex())) {
            devicedataList = dmcYingShiCameraService.selectDeviceData(selectDeviceDataDto);
        }

        if (deviceType.equals(DmcDeviceTypeEnum.MOHE_GATEWAY.getIndex()) ||
                deviceType.equals(DmcDeviceTypeEnum.INFRARED_DETECTOR.getIndex()) ||
                deviceType.equals(DmcDeviceTypeEnum.OVERFLOW_ALARM.getIndex()) ||
                deviceType.equals(DmcDeviceTypeEnum.EMERGENCY_CALLER.getIndex()) ||
                deviceType.equals(DmcDeviceTypeEnum.DOOR_SENSOR.getIndex()) ||
                deviceType.equals(DmcDeviceTypeEnum.SMOKE_ALARM.getIndex()) ||
                deviceType.equals(DmcDeviceTypeEnum.GAS_ALARM.getIndex())) {
            devicedataList = alarmLightLocationService.selectDeviceData(selectDeviceDataDto);
        }

        //写入设备所在机构管管理员账号
        for (PropertyDataVo propertyDataVo : devicedataList) {
            String longinName = (String) redisTemplate.opsForValue().get(RedisKeyUtil.getUacGroupLoginname(propertyDataVo.getGroupId().toString()));
            String groupName = (String) redisTemplate.opsForValue().get(RedisKeyUtil.getUacGroupName(propertyDataVo.getGroupId().toString()));

            propertyDataVo.setLoginName(longinName);
            propertyDataVo.setGroupName(groupName);

            //如果是个人绑定,写入个人姓名
            if (propertyDataVo.getGroupId().equals(1000L)) {
                DmcDeviceUserBind dmcDeviceUserBind = new DmcDeviceUserBind();
                dmcDeviceUserBind.setDeviceId(propertyDataVo.getDeviceId());
                DmcDeviceUserBind deviceUserBind = userBindService.selectOne(dmcDeviceUserBind);

                if (PublicUtil.isNotEmpty(deviceUserBind)) {
                    String userNameAndLoginName = (String) redisTemplate.opsForValue().get(RedisKeyUtil.getUacUserName(deviceUserBind.getUserId().toString()));
                    if (PublicUtil.isNotEmpty(userNameAndLoginName)) {
                        String[] split = userNameAndLoginName.split("-");
                        propertyDataVo.setUserLoginName(split[1]);
                    }
                }
            }
        }
        PageInfo pageInfo = new PageInfo<>(devicedataList);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), pageInfo);
    }


    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "绑定商家与设备")
    @RequestMapping(value = "/bindDeviceMerchant", method = RequestMethod.POST)
    public Wrapper bindDeviceMerchant(@RequestBody MerchantSaveDeviceDto merchantSaveDeviceDto) {


        DmcDevice device = new DmcDevice();
        device.setDeviceNo(merchantSaveDeviceDto.getDeviceNo());
        List<DmcDevice> select = dmcDeviceService.select(device);

        //校验此设备是否存在
        if (PublicUtil.isEmpty(select)) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("excel.deviceNo.sql.null", null));
        }

        for (DmcDevice dmcDevice : select) {

            //校验此设备是否被此机构购买
            DmcPropertyManagement dmcPropertyManagement = propertyManagementService.selectByKey(dmcDevice.getId());
            if (!dmcPropertyManagement.getBuyGroupId().equals(getLoginAuthDto().getGroupId())) {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("managent.device.error", null) + "-----" + dmcDevice.getDeviceNo());
            }
            //校验设备是否被绑定过
            if (dmcDevice.getGroupId() != 0) {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("device.bing.group.error", null));
            }

        }

        //绑定商家与设备
        int result = dmcDeviceService.bindOnedeviceGroup(merchantSaveDeviceDto, getLoginAuthDto().getGroupId());

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), result);
    }


    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "商家批量删除设备")
    @RequestMapping(value = "/untie", method = RequestMethod.POST)
    public Wrapper untie(@RequestBody DeviceDeriveExcelDto deriveExcelDto) {


        for (Long id : deriveExcelDto.getDeviceIds()) {

            DmcDevice dmcDevice = dmcDeviceService.selectByKey(id);
            DmcPropertyManagement dmcPropertyManagement = propertyManagementService.selectByKey(id);

            //判定设备是否被机构购买
            if (!dmcPropertyManagement.getBuyGroupId().equals(getLoginAuthDto().getGroupId())) {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("managent.device.notbuy", null) + ":" + dmcDevice.getDeviceNo());
            }
            //判定设备是否被机构启用
            if (!dmcDevice.getGroupId().equals(getLoginAuthDto().getGroupId())) {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("managent.device.notbind", null) + ":" + dmcDevice.getDeviceNo());
            }

            //判断设备是否被个人绑定
            DmcDeviceUserBind deviceUserBind = new DmcDeviceUserBind();
            deviceUserBind.setDeviceId(id);
            List<DmcDeviceUserBind> dmcDeviceUserBinds = userBindService.select(deviceUserBind);
            if (PublicUtil.isNotEmpty(dmcDeviceUserBinds)) {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("device.userbind.error", null) + ":" + dmcDevice.getDeviceNo());
            }


            //判定设备是否在内部进行了绑定
            Integer deviceType = dmcDevice.getDeviceType();

            //巡更棒
            if (deviceType.equals(DmcDeviceTypeEnum.PATROL.getIndex())) {
                List<DmcPatrolPointBind> dmcPatrolPointBinds = dmcPatrolGreatLocationServiceImp.selectPatrolPoint(id);
                if (dmcPatrolPointBinds.size() != 0) {
                    return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("patrol.bind.point.error", null) + ":" + dmcDevice.getDeviceNo());
                }
                /*
                 * 删除巡更棒设置的巡更时间
                 * */
                dmcPatrolGreatConfigServiceImp.deleteByDeviceId(id);
            }
            //巡更点
            if (deviceType.equals(DmcDeviceTypeEnum.PATROL_POINT.getIndex())) {
                PatrolPageQuery patrolPageQuery = new PatrolPageQuery();
                patrolPageQuery.setDeviceId(id);
                List<PatrolPageQuery> patrolPageQueries = dmcPatrolGreatLocationServiceImp.selectBindPatrol(patrolPageQuery);
                if (patrolPageQueries.size() != 0) {
                    return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("point.bind.patrol.error", null) + ":" + dmcDevice.getDeviceNo());
                }
            }

            //孝心手环
            if (deviceType.equals(DmcDeviceTypeEnum.XX_WATCH.getIndex())) {
                DmcWatchOldman dmcWatchOldman = watchOldmanService.selectByKey(id);
                if (PublicUtil.isNotEmpty(dmcWatchOldman)) {
                    return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("managent.device.bind.error", null) + ":" + dmcDevice.getDeviceNo());
                }

            }
            //电话机
            if (deviceType.equals(DmcDeviceTypeEnum.ZN_PHONE.getIndex())) {
                DmcPhoneLocation phoneLocation = phoneLocationService.selectByKey(id);
                if (PublicUtil.isNotEmpty(phoneLocation)) {
                    return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("managent.device.bind.error", null) + ":" + dmcDevice.getDeviceNo());
                }
            }
            //智能网关
            if (deviceType.equals(DmcDeviceTypeEnum.CSUN_GATEWAY.getIndex())) {
                //校验网关与位置的绑定是否解除
                DmcCsunGatewayLocation dmcCsunGatewayLocation = gatewayLocationService.selectByKey(id);
                if (PublicUtil.isNotEmpty(dmcCsunGatewayLocation)) {
                    return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("managent.device.bind.error", null) + ":" + dmcDevice.getDeviceNo());
                }
                //校验网关与设备的绑定是否解除
                DmcCsunGatewayBind dmcCsunGatewayBind = new DmcCsunGatewayBind();
                dmcCsunGatewayBind.setGatewayId(id);
                List<DmcCsunGatewayBind> select = gatewayBindService.select(dmcCsunGatewayBind);
                if (PublicUtil.isNotEmpty(select)) {
                    return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("managent.device.bind.error", null) + ":" + dmcDevice.getDeviceNo());
                }
            }
            //紧急按钮
            if (deviceType.equals(DmcDeviceTypeEnum.CSUN_BUTTON.getIndex())) {
                DmcCsunButtonLocation buttonLocation = buttonLocationService.selectByKey(id);
                if (PublicUtil.isNotEmpty(buttonLocation)) {
                    return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("managent.device.bind.error", null) + ":" + dmcDevice.getDeviceNo());
                }
            }
            //人体感应
            if (deviceType.equals(DmcDeviceTypeEnum.CSUN_BODY_SENSOR.getIndex())) {
                DmcCsunBodySensorLocation dmcCsunBodySensorLocation = bodySensorLocationService.selectByKey(id);
                if (PublicUtil.isNotEmpty(dmcCsunBodySensorLocation)) {
                    return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("managent.device.bind.error", null) + ":" + dmcDevice.getDeviceNo());
                }
                //校验网关与设备的绑定是否删除
                DmcCsunGatewayBind dmcCsunGatewayBind = new DmcCsunGatewayBind();
                dmcCsunGatewayBind.setDeviceId(id);
                List<DmcCsunGatewayBind> select = gatewayBindService.select(dmcCsunGatewayBind);
                if (PublicUtil.isNotEmpty(select)) {
                    return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("managent.device.bind.error", null) + ":" + dmcDevice.getDeviceNo());
                }

                // 判断是否绑定了人感组
                DmcCsunBodyGroupBind dmcCsunBodyGroupBind = new DmcCsunBodyGroupBind();
                dmcCsunBodyGroupBind.setDeviceId(id);
                int count = dmcCsunBodyGroupBindService.selectCount(dmcCsunBodyGroupBind);
                if (count > 0) {
                    throw new DmcBizException(ErrorCodeEnum.DMC100614011);
                }
            }
            //门禁读卡器
            if (deviceType.equals(DmcDeviceTypeEnum.RFID_READER.getIndex())) {
                DmcRfidReaderLocation dmcRfidReaderLocation = rfidReaderLocationService.selectByKey(id);
                if (PublicUtil.isNotEmpty(dmcRfidReaderLocation)) {
                    return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("managent.device.bind.error", null) + ":" + dmcDevice.getDeviceNo());
                }
            }
            //门禁标签
            if (deviceType.equals(DmcDeviceTypeEnum.RFID_LABEL.getIndex())) {
                DmcRfidLabelOldman rfidLabelOldman = rfidLabelOldmanService.selectByKey(id);
                if (PublicUtil.isNotEmpty(rfidLabelOldman)) {
                    return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("managent.device.bind.error", null) + ":" + dmcDevice.getDeviceNo());
                }
            }
            //日创床垫
            if (deviceType.equals(DmcDeviceTypeEnum.CSUN_SLEEP.getIndex())) {
                DmcCsunSleepBind csunSleepBind = sleepBindService.selectByKey(id);
                if (PublicUtil.isNotEmpty(csunSleepBind)) {
                    return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("managent.device.bind.error", null) + ":" + dmcDevice.getDeviceNo());
                }
            }

            //报警灯
            if (deviceType.equals(DmcDeviceTypeEnum.ALARM_LIGHT.getIndex())) {
                DmcAlarmLightLocation dmcAlarmLightLocation = alarmLightLocationService.selectByKey(id);
                if (PublicUtil.isNotEmpty(dmcAlarmLightLocation)) {
                    return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("managent.device.bind.error", null) + ":" + dmcDevice.getDeviceNo());
                }
            }
            //智能摄像头
            if (deviceType.equals(DmcDeviceTypeEnum.CSUN_CAMERA.getIndex())) {
                DmcCsunCameraLocation dmcCsunCameraLocation = cameraLocationService.selectByKey(id);
                if (PublicUtil.isNotEmpty(dmcCsunCameraLocation)) {
                    return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("managent.device.bind.error", null) + ":" + dmcDevice.getDeviceNo());
                }
            }
            //床头屏
            if (deviceType.equals(DmcDeviceTypeEnum.BEDSIDE_SCREEN.getIndex())) {
                DmcBedsideCareBind dmcBedsideCareBind = bedsideScreenBindService.selectByKey(id);
                if (PublicUtil.isNotEmpty(dmcBedsideCareBind)) {
                    return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("managent.device.bind.error", null) + ":" + dmcDevice.getDeviceNo());
                }
            }
            //温度计
            if (deviceType.equals(DmcDeviceTypeEnum.HUMIDISTAT.getIndex())) {

                //判定是否进行了位置绑定
                DmcCsunHumidistatLocation dmcCsunHumidistatLocation = humidistatLocationService.selectByKey(id);
                if (PublicUtil.isNotEmpty(dmcCsunHumidistatLocation)) {

                    return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("managent.device.bind.error", null) + ":" + dmcDevice.getDeviceNo());
                }

                //判断温度计是否与网关绑定
                DmcCsunGatewayBind dmcCsunGatewayBind = new DmcCsunGatewayBind();
                dmcCsunGatewayBind.setDeviceId(id);
                List<DmcCsunGatewayBind> select = gatewayBindService.select(dmcCsunGatewayBind);
                if (PublicUtil.isNotEmpty(select)) {

                    return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("managent.device.bind.error", null) + ":" + dmcDevice.getDeviceNo());
                }

            }
            //日创新人感
            if (deviceType.equals(DmcDeviceTypeEnum.NEWBODY.getIndex())) {
                //判断是否进行了位置绑定
                DmcCsunBodySensorLocation bodySensorLocation = bodySensorLocationService.selectByKey(id);
                if (PublicUtil.isNotEmpty(bodySensorLocation)) {
                    return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("managent.device.bind.error", null) + ":" + dmcDevice.getDeviceNo());
                }

                //判断是否绑定了网关
                DmcCsunGatewayBind dmcCsunGatewayBind = new DmcCsunGatewayBind();
                dmcCsunGatewayBind.setDeviceId(id);
                List<DmcCsunGatewayBind> select = gatewayBindService.select(dmcCsunGatewayBind);
                if (PublicUtil.isNotEmpty(select)) {
                    return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("managent.device.bind.error", null) + ":" + dmcDevice.getDeviceNo());
                }
            }
            //日创门磁
            if (deviceType.equals(DmcDeviceTypeEnum.CSUN_DOOR.getIndex())) {
                DmcCsunDoorLocation dmcCsunDoorLocation = doorLocationService.selectByKey(id);
                if (PublicUtil.isNotEmpty(dmcCsunDoorLocation)) {
                    return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("managent.device.bind.error", null) + ":" + dmcDevice.getDeviceNo());
                }
            }

        }


        int result = dmcDeviceService.deleteuntie(deriveExcelDto.getDeviceIds());

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), result);
    }


    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "商品出库给商家")
    @RequestMapping(value = "/delivery", method = RequestMethod.POST)
    public Wrapper delivery(@RequestBody DeviceDeliveryDto deviceDeliveryDto) {

        //校验设备是否被出库过
        List<DmcPropertyManagement> propertyManagements = propertyManagementService.selectByIds(deviceDeliveryDto.getIds());
        for (DmcPropertyManagement propertyManagement : propertyManagements) {
            if (propertyManagement.getBuyGroupId() != 0) {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("device.delivery.error", null));
            }
        }

        int result = propertyManagementService.delivery(deviceDeliveryDto, deviceDeliveryDto.getShipmentDate());


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), result);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "物管平台数据同步")
    @RequestMapping(value = "/synchronization", method = RequestMethod.POST)
    public Wrapper uiuiui() {

        List<DmcDevice> devices = dmcDeviceService.selectAll();
        List<DmcPropertyManagement> propertyManagements = propertyManagementService.selectAll();
        List<DmcPropertyManagement> ps = new ArrayList<>();
        List<Long> prodevices = new ArrayList<>();
        int update = 0;


        for (DmcPropertyManagement propertyManagement : propertyManagements) {
            prodevices.add(propertyManagement.getDeviceId());
        }
        for (Long aLong : prodevices) {
            for (int i = 0; i < devices.size(); i++) {
                if (devices.get(i).getId().equals(aLong)) {
                    devices.remove(i);

                }
            }
        }

        for (DmcDevice device : devices) {
            DmcPropertyManagement dmcPropertyManagement = new DmcPropertyManagement();
            dmcPropertyManagement.setDeviceId(device.getId());
            dmcPropertyManagement.setBuyGroupId(device.getGroupId());
            dmcPropertyManagement.setBatch("1");
            dmcPropertyManagement.setManagentNo(device.getDeviceNo() + "-" + dmcPropertyManagement.getBatch());
            dmcPropertyManagement.setProductionDate(new Date());
            dmcPropertyManagement.setInitializeDate(new Date());
            dmcPropertyManagement.setShipmentDate(new Date());

            ps.add(dmcPropertyManagement);
        }

        for (DmcPropertyManagement p : ps) {
            int save = propertyManagementService.save(p);
            update++;
        }


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), update);
    }
   /* @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "数据改变脚本")
    @RequestMapping(value = "/saas", method = RequestMethod.POST)
    public Wrapper saas() {

        List<DmcDevice> devices = dmcDeviceService.selectAll();
        List<DmcPropertyManagement> propertyManagements = propertyManagementService.selectAll();
        int update=0;

        List<DmcPropertyManagement>lists=new ArrayList<>();
        for (int i = 0; i < propertyManagements.size(); i++) {
            if (propertyManagements.get(i).getBuyGroupId()==0){
                lists.add(propertyManagements.get(i));
            }
        }

        for (DmcPropertyManagement propertyManagement : lists) {
            for (int i = 0; i < devices.size(); i++) {
                if(propertyManagement.getDeviceId().equals(devices.get(i).getId())){
                    DmcPropertyManagement dmcPropertyManagement = new DmcPropertyManagement();
                    dmcPropertyManagement.setDeviceId(devices.get(i).getId());
                    dmcPropertyManagement.setBuyGroupId(devices.get(i).getGroupId());
                    update += propertyManagementService.update(dmcPropertyManagement);
                }
            }
        }


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),update);
    }*/

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "商品退回")
    @RequestMapping(value = "/sendBack", method = RequestMethod.POST)
    public Wrapper sendBack(@RequestBody DeviceDeriveExcelDto deviceDeriveExcelDto) {

        List<PropertyManagementStausVo> list = propertyManagementService.selectactivateBydiveceIds(deviceDeriveExcelDto.getDeviceIds());


        for (PropertyManagementStausVo propertyManagementStausVo : list) {

            //校验该设备是否处于出库状态
            if (propertyManagementStausVo.getBuyGroupId() == 0) {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("device.sendBack.no.error", null) + "---------" + propertyManagementStausVo.getDeviceNo());
            }

            //校验商家是否删除该设备
            if (propertyManagementStausVo.getGroupId() != 0) {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("device.delete.sendBack.error", null) + "---------" + propertyManagementStausVo.getDeviceNo());
            }

        }

        //商品回库
        int relust = propertyManagementService.sendBack(deviceDeriveExcelDto.getDeviceIds());


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), relust);
    }
}
